//
//  NWDisView.swift
//  微博01
//
//  Created by 牛威 on 15/12/6.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit

class NWDisView: UIView {

    @IBOutlet weak var textLabel: UITextField!
    
    @IBOutlet weak var rightConstant: NSLayoutConstraint!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
   //实现text的代理方法
    
    
    
  
    //点击取消恢复原状
    
    @IBAction func cancelClick(sender: AnyObject) {
        
       self.textLabel.resignFirstResponder()
        
        self.rightConstant.constant = 0
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.layoutIfNeeded()
            
        }
        
        
    }
    
    
    //MARK: - 加载xib
    class func searchBar()->NWDisView {
        
        return NSBundle.mainBundle().loadNibNamed("NWDisView", owner: nil, options: nil).last! as! NWDisView
    }
    
    override func awakeFromNib() {
        
        
        textLabel.layer.borderColor = UIColor.cyanColor().CGColor
        
        textLabel.layer.borderWidth = 3
        
        textLabel.layer.cornerRadius = 5
        
        
        let imageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon")?.imageWithRenderingMode(.AlwaysOriginal))
        
//        textLabel.leftViewRectForBounds(CGRectMake(50, 0, frame.height, frame.height))
        
        
//        imageView.sizeToFit()
        

        
//       textLabel.leftView?.frame = CGRectMake(0, 0, frame.height, frame.height)
        
        textLabel.leftView = imageView
        
        
        textLabel.leftViewMode = UITextFieldViewMode.Always
        
        textLabel.contentMode = .Center
        
        
        textLabel.background = UIImage(named: "searchbar_textfield_background")
    }
    
    
    


}




extension NWDisView :  UITextFieldDelegate {

  
    func textFieldDidBeginEditing(textField: UITextField) {
        
        
        self.rightConstant.constant = self.cancelBtn.width
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.layoutIfNeeded()
        }
    }


}
